package top.lgd.controller.person;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lgd.base.LgdConstants;
import top.lgd.base.LgdResult;
import top.lgd.base.LgdTable;
import top.lgd.controller.LgdController;
import top.lgd.pojo.entity.person.Salary;
import top.lgd.service.person.BalanceService;
import top.lgd.service.person.SalaryService;

/**
 * @author YYJ
 * @version 1.0
 * @since 2023/12/19 19:12
 */
@RestController
@RequestMapping("/admin/personSalary")
public class SalaryAdminController extends LgdController {

    @Autowired
    private SalaryService salaryService;
    @Autowired
    private BalanceService balanceService;

    /**
     * 查询工资收入列表
     *
     * @return
     */
    @PostMapping("/queryList")
    public LgdTable<Salary> queryList(@RequestBody Salary salary) {
        QueryWrapper<Salary> queryWrapper = new QueryWrapper(salary);
        if (null != salary.getStartDate()) {
            queryWrapper.ge(LgdConstants.COLUMN_SALARY_DATE, salary.getStartDate());
        }
        if (null != salary.getStartDate()) {
            queryWrapper.le(LgdConstants.COLUMN_SALARY_DATE, salary.getEndDate());
        }
        queryWrapper.eq(LgdConstants.COLUMN_DATA_USER_ID, current().getId())
                .orderByDesc(LgdConstants.COLUMN_SALARY_DATE);
        Page<Salary> page = salaryService.page(new Page(salary.getCurrent(), salary.getPageSize()), queryWrapper);
        return getTable(page);
    }

    /**
     * 添加编辑工资收入
     *
     * @param salary 工资收入 对象
     * @return
     */
    @PostMapping("add")
    public LgdResult<Salary> add(@RequestBody Salary salary) {
        salary.setDataUserId(current().getId());
        salaryService.addAndFlow(salary);
        balanceService.staticBalance(current().getId());
        return LgdResult.success(salary);
    }


}